<?php
/**
 * 会员推荐产品销售日志管理
 *
 *
 *
 * * @网店运维 (c) 2015-2018 ShopWWI Inc. (http://www.shopwwi.com)
 * @license    http://www.shopwwi.c om
 * @link       交流群号：111731672
 * @since      网店运维提供技术支持 授权请购买shopnc授权
 */
defined('ByShopWWI') or exit('Access Invalid!');

class saleModel extends Model {
    
   
    public function __construct(){
        parent::__construct('sale_record');
    }
   
    /**
     * 保存销售记录
     */
    public function saveSaleRecord($order_id){
        
       
        $model_order= Model('order');
        $model_member= Model('member');
        $model_pd = Model('predeposit');
        $model_brokerage= Model('brokerage');
        $order_info=$model_order->getOrderInfo(array('order_id'=>$order_id),array('order_goods'));
        $extend_order_goods=$order_info['extend_order_goods'];
        
        foreach($extend_order_goods as $goods)
        {
            $value_array = array();
            $value_array['recommend_id'] = $goods['recommend_id'];
            $value_array['goods_id'] = $goods['goods_id'];
            $value_array['goods_name'] = $goods['goods_name'];
            $value_array['goods_num'] = $goods['goods_num'];
            $value_array['sale_amount'] = ncPriceFormat($goods['goods_price']*$goods['goods_num']);
            $value_array['order_id'] = $order_info['order_id'];
            $value_array['order_sn'] = $order_info['order_sn'];                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
            $value_array['buyer_id'] = $order_info['buyer_id'];
            $value_array['buyer_name'] = $order_info['buyer_name'];
            $value_array['order_time'] = $order_info['add_time'];
            $result=0;
            if($value_array['goods_num'] != '0'){
                $result = $this->addSaleRecord($value_array);
            }
            if ($result)
            {
                //账户余额变动日志,对推荐产品的会员进行返利1%
                if(!empty($value_array['recommend_id']))
                {
                    $data = array();
                    $data['member_id']=$value_array['recommend_id'];
                    $data['goods_id']=$value_array['goods_id'];
                    $data['order_sn']=$value_array['order_sn'];
                    $data['buyer_name']=$value_array['buyer_name'];
                    $data['amount']=$value_array['sale_amount']*0.01;
                    $model_pd->changePd('sales_commission',$data);
                }
                $member=$model_member->getMemberInfo(array('member_id'=>$value_array['buyer_id']));
                //向上返还分销佣金
                if($member['invite_one']>0&&$goods['brokerage_one']>0)
                {
                    $data = array();
                    $data['member_id']=$member['invite_one'];
                    $data['goods_id']=$value_array['goods_id'];
                    $data['order_sn']=$value_array['order_sn'];
                    $data['buyer_name']=$value_array['buyer_name'];
                    $data['amount']=$goods['brokerage_one'];
                    $model_pd->changePd('back_brokerage',$data);
                    $temp=array();
                    $temp['inviter_id']=$member['invite_one'];
                    $temp['goods_id']=$value_array['goods_id'];
                    $temp['goods_name']=$value_array['goods_name'];
                    $temp['order_sn']=$value_array['order_sn'];
                    $temp['buyer_id']=$value_array['buyer_id'];
                    $temp['buyer_name']=$value_array['buyer_name'];
                    $temp['buyer_floor']=1;
                    $temp['brokerage_amount']=$goods['brokerage_one'];
                    $temp['order_time']=$value_array['order_time'];
                    $model_brokerage->addBrokerageRecord($temp);
                    
                }
                if($member['invite_two']>0&&$goods['brokerage_two']>0)
                {
                    $data = array();
                    $data['member_id']=$member['invite_two'];
                    $data['goods_id']=$value_array['goods_id'];
                    $data['order_sn']=$value_array['order_sn'];
                    $data['buyer_name']=$value_array['buyer_name'];
                    $data['amount']=$goods['brokerage_two'];
                    $model_pd->changePd('back_brokerage',$data);
                    $temp=array();
                    $temp['inviter_id']=$member['invite_two'];
                    $temp['goods_id']=$value_array['goods_id'];
                    $temp['goods_name']=$value_array['goods_name'];
                    $temp['order_sn']=$value_array['order_sn'];
                    $temp['buyer_id']=$value_array['buyer_id'];
                    $temp['buyer_name']=$value_array['buyer_name'];
                    $temp['buyer_floor']=2;
                    $temp['brokerage_amount']=$goods['brokerage_two'];
                    $temp['order_time']=$value_array['order_time'];
                    $model_brokerage->addBrokerageRecord($temp);
                }
                if($member['invite_three']>0&&$goods['brokerage_three']>0)
                {
                    $data = array();
                    $data['member_id']=$member['invite_three'];
                    $data['goods_id']=$value_array['goods_id'];
                    $data['order_sn']=$value_array['order_sn'];
                    $data['buyer_name']=$value_array['buyer_name'];
                    $data['amount']=$goods['brokerage_three'];
                    $model_pd->changePd('back_brokerage',$data);
                    $temp=array();
                    $temp['inviter_id']=$member['invite_three'];
                    $temp['goods_id']=$value_array['goods_id'];
                    $temp['goods_name']=$value_array['goods_name'];
                    $temp['order_sn']=$value_array['order_sn'];
                    $temp['buyer_id']=$value_array['buyer_id'];
                    $temp['buyer_name']=$value_array['buyer_name'];
                    $temp['buyer_floor']=3;
                    $temp['brokerage_amount']=$goods['brokerage_three'];
                    $temp['order_time']=$value_array['order_time'];
                    $model_brokerage->addBrokerageRecord($temp);
                }
            }
            
        }
  

    }
    
    
    
     /**
     * 添加产品销售日志信息
     *
     * @param array $param 添加信息数组
     */
    public function addSaleRecord($param) {
        if(empty($param)) {
            return false;
        }
        return $this->insert($param);
    }
    /**
     * 产品销售日志列表
     *
     * @param unknown $condition
     * @param string $pagesize
     * @param string $fields
     * @param string $order
     */
    public function getSaleRecordList($condition = array(), $pagesize = '', $fields = '*', $order = '', $limit = ''){
        return $this->where($condition)->field($fields)->order($order)->limit($limit)->page($pagesize)->select();
    }
    
     /**
     * 取日志总数
     * @param unknown $condition
     */
    public function getSaleRecordCount($condition = array()) {
        return $this->where($condition)->count();
    }
    
    
    /**
     * 产品销售日志详细信息
     *
     * @param array $condition 条件数组
     * @param array $field   查询字段
     */
    public function getSaleRecordInfo($where = array(), $field = '*', $order = '',$group = ''){
        $info = $this->where($where)->field($field)->order($order)->group($group)->find();
        if (!$info){
            return array();
        }
        return $info;
    }
}
